package com.yang.dao.impl;

import com.yang.dao.DataStatisticeDao;
import com.yang.entity.*;
import com.yang.pojo.GuestDto;
import com.yang.service.impl.DataStatisticServiceImpl;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Repository
public class DataStatisticeDaoImpl implements DataStatisticeDao {

    public static Logger logger = Logger.getLogger(DataStatisticServiceImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public List<OrderFormEntity> selectOrderByTime(long beginTime, long overTime) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(OrderFormEntity.class);
        criteria.add(Restrictions.ge("beginDay",beginTime));
        criteria.add(Restrictions.le("beginDay",overTime));
        List<OrderFormEntity> orderFormEntities = criteria.list();
        session.close();
        return orderFormEntities;
    }

    @Override
    public LiverRelationEntity selectLiveByOrderId(long orderId) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(LiverRelationEntity.class);
        criteria.add(Restrictions.eq("orderFormId",orderId));
        LiverRelationEntity liverRelationEntity = (LiverRelationEntity) criteria.uniqueResult();
        session.close();
        return liverRelationEntity;
    }



    @Override
    public RoomEntity selectRoomById(long id) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(RoomEntity.class);
        criteria.add(Restrictions.eq("id",id));
        RoomEntity roomEntity = (RoomEntity) criteria.uniqueResult();
        session.close();
        return roomEntity;
    }

    @Override
    public GuestEntity selectGuestById(long guestId ,String status) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(GuestEntity.class);
        criteria.add(Restrictions.eq("id",guestId));
        criteria.add(Restrictions.eq("status",status));
        GuestEntity guestEntity = (GuestEntity) criteria.uniqueResult();
        session.close();
        return guestEntity;
    }

    @Override
    public List<GuestDto> selectGuestByorderId(long orderId, String status) {
        String sql = "SELECT name,idetification,tel FROM guest WHERE id IN (SELECT guest_id FROM liver_relation WHERE order_form_id = ?) AND status = ?";
        ResultSetExtractor<List<GuestDto>> lists = new ResultSetExtractor<List<GuestDto>>() {
            @Override
            public List<GuestDto> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<GuestDto> lists = new ArrayList<GuestDto>();
                while(rs.next()) {
                    GuestDto guestDto = new GuestDto();
                    guestDto.setName(rs.getString("name"));
                    guestDto.setIdentity(rs.getString("idetification"));
                    guestDto.setTel(rs.getString("tel"));
                    logger.info("数据库查找到 = "+guestDto.toString());
                    lists.add(guestDto);
                }
                return lists;
            }
        };
        return jdbcTemplate.query(sql,new Object[] {orderId,status},lists);
    }

    @Override
    public RelationEntity selectRelationByOrderIdAndGuestStatic(long orderId,String guestStatic) {
        String hql = "FROM RelationEntity WHERE orderFormId.id = ? AND guestId.status = ?";
        List<RelationEntity> relationEntities = (List<RelationEntity>) hibernateTemplate.find(hql,new Object[]{orderId,guestStatic});
        return relationEntities.isEmpty()?null:relationEntities.get(0);
    }
}
